package com.glsc.ngateway.opmanage.service.ledger;


import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthStaff;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.ledger.LedgerAuthStaffRepository;
import com.glsc.ngateway.opmanage.service.UserService;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class LedgerAuthStaffService {

    @Resource
    private LedgerAuthStaffRepository ledgerAuthStaffRepository;


    @Resource
    private UserService userService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<LedgerAuthStaff> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<LedgerAuthStaff> page = ledgerAuthStaffRepository.findAll(SpecificationUtil.buildSpecification(param, LedgerAuthStaff.class), pageable);

        return page;
    }

    /**
     * 根据权限id查找所有的员工信息
     *
     * @param authId
     * @return
     */
    public List<LedgerAuthStaff> listByAuthId(Integer authId) {
        List<LedgerAuthStaff> staffs = ledgerAuthStaffRepository.findAll(Example.of(LedgerAuthStaff.builder().ledgerAuthId(authId).build()));
        return staffs;
    }

    /**
     * 根据用户id获取所有的员工和权限id信息
     *
     * @param userOaAccount
     * @return
     */
    public List<LedgerAuthStaff> listByUserOaAccount(String userOaAccount) {
        List<LedgerAuthStaff> staffs = ledgerAuthStaffRepository.findAll(Example.of(LedgerAuthStaff.builder().userOaAccount(userOaAccount).build()));
        return staffs;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public LedgerAuthStaff findById(Integer id) {
        Optional<LedgerAuthStaff> optional = ledgerAuthStaffRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public LedgerAuthStaff save(LedgerAuthStaff obj) {
        //不能重复新增
        User user = userService.findByUserAccount(obj.getUserOaAccount());
        obj.setUserId(user.getId().intValue());

        List<LedgerAuthStaff> staffs = ledgerAuthStaffRepository.findAll(Example.of(LedgerAuthStaff.builder().userId(obj.getUserId()).ledgerAuthId(obj.getLedgerAuthId()).build()));
        if (CollectionUtils.isNotEmpty(staffs)) {
            obj.setId(staffs.get(0).getId());
        }

        obj.setUserName(user.getName());

        LedgerAuthStaff res = ledgerAuthStaffRepository.save(obj);
        return res;
    }


    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        ledgerAuthStaffRepository.deleteById(id);
    }

    /**
     * 根据id批量删除
     *
     * @param ids
     */
    public void deleteByIds(List<Integer> ids) {
        ledgerAuthStaffRepository.deleteAllById(ids);
    }


}
